/**
 * File Name	: TripleDESCipher.java
 * Author		: Administrator:Adelwin
 * Create Date	: Mar 30, 2006:11:20:08 AM
 *
 * Copyright (c) 2006 Adelwin. All Rights Reserved. <BR>
 * <BR>
 * This software contains confidential and proprietary information of
 * Adelwin. ("Confidential Information").<BR>
 * <BR>
 * Such Confidential Information shall not be disclosed and it shall
 * only be used in accordance with the terms of the license agreement
 * entered into with Solveware Independent; other than in accordance with the written
 * permission of Solveware Independent. <BR>
 *
 *
 */

package com.uobgroup.cmt.base.util;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

import com.uobgroup.cmt.base.exception.CipherException;

public class TripleDESCipher extends CipherWrapper {

	private Cipher cipher = null;
	
	public TripleDESCipher() throws NoSuchAlgorithmException, NoSuchPaddingException {
		cipher = Cipher.getInstance("TripleDES/ECB/PKCS5Padding");		
	}
	
	public byte[] encrypt(byte[] input) throws CipherException {
		throw new CipherException("Key must be Supplied");
	}
	public byte[] encrypt(byte[] input, byte[] key) throws CipherException {
		if (input == null || key == null) {
			throw new IllegalArgumentException("The Key or Input is null");
		}
		try {
			SecretKey secretKey = SecretKeyFactory.getInstance("TripleDES").generateSecret(new DESedeKeySpec(key));
			return encrypt(input, secretKey);
		} catch (InvalidKeyException e) {
			throw new CipherException(e.getMessage(), e);
		} catch (InvalidKeySpecException e) {
			throw new CipherException(e.getMessage(), e);
		} catch (NoSuchAlgorithmException e) {
			throw new CipherException(e.getMessage(), e);
		}
	}
	public byte[] encrypt(byte[] input, Key key) throws CipherException {
		if (input == null || key == null) {
			throw new IllegalArgumentException("The Key or Input is null");
		}
		try {
			cipher.init(Cipher.ENCRYPT_MODE, key);
			return cipher.doFinal(input);
		} catch (InvalidKeyException e) {
			throw new CipherException(e.getMessage(), e);
		} catch (IllegalStateException e) {
			throw new CipherException(e.getMessage(), e);
		} catch (IllegalBlockSizeException e) {
			throw new CipherException(e.getMessage(), e);
		} catch (BadPaddingException e) {
			throw new CipherException(e.getMessage(), e);
		}
	}

	public byte[] decrypt(byte[] input) throws CipherException {
		throw new CipherException("Key must be Supplied");
	}
	public byte[] decrypt(byte[] input, byte[] key) throws CipherException {
		if (input == null || key == null) {
			throw new IllegalArgumentException("The Key or Input is null");
		}
		try {
			SecretKey secretKey = SecretKeyFactory.getInstance("TripleDES").generateSecret(new DESedeKeySpec(key));
			return decrypt(input, secretKey);
		} catch (InvalidKeyException e) {
			throw new CipherException(e.getMessage(), e);
		} catch (InvalidKeySpecException e) {
			throw new CipherException(e.getMessage(), e);
		} catch (NoSuchAlgorithmException e) {
			throw new CipherException(e.getMessage(), e);
		}
	}
	public byte[] decrypt(byte[] input, Key key) throws CipherException {
		if (input == null || key == null) {
			throw new IllegalArgumentException("The Key or Input is null");
		}
		try {
			cipher.init(Cipher.DECRYPT_MODE, key);
			return cipher.doFinal(input);
		} catch (InvalidKeyException e) {
			throw new CipherException(e.getMessage(), e);
		} catch (IllegalStateException e) {
			throw new CipherException(e.getMessage(), e);
		} catch (IllegalBlockSizeException e) {
			throw new CipherException(e.getMessage(), e);
		} catch (BadPaddingException e) {
			throw new CipherException(e.getMessage(), e);
		}
	}

}
